package com.frank.http.expand;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.frank.http.base.AsyncHttpClient;
import com.frank.http.base.AsyncHttpResponseHandler;
import com.frank.http.base.RequestParams;
import com.frank.http.encryp.EncrypUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

/**
 * 初始化类
 * 
 * @author Frank
 * @version 1.0 Create by 2016.03.30
 */
public class InitUitls {
	private final static String TAG = InitUitls.class.getSimpleName();
	public static final String SESSION_ID = "session_id";
	public static final String SESSION_CARD = "session_card";
	public static final String SESSION_KEY = "session_key";
	public static final String SESSION_SIGN = "session_sign";
	public static final String SESSION_ID_NAME = "session_id_name";
	public static final String SESSION_CARD_NAME = "session_card_name";
	public static final String SESSION_KEY_NAME = "session_key_name";
	public static final String SESSION_SIGN_NAME = "session_sign_name";
	public static final String SESSION_HEADERS_PREFIX = "session_prefix";
	public static final String SESSION_URL = "session_url";

	/**
	 * 设置初始化路径
	 */
	public static void setInitSessionUrl(String url) {
		PreferenceAppService.getInstance().setItemAndroid(SESSION_URL, url);
	}

	/**
	 * 获得初始化路径
	 */
	public static String getInitSessionUrl() {
		return PreferenceAppService.getInstance().getItemAndroid(SESSION_URL)
				.equals("0") ? "" : PreferenceAppService.getInstance().getItemAndroid(
				SESSION_URL);
	}

	/**
	 * 初始化网络请求
	 * 
	 * @param context
	 * @param url
	 */
	public static void initSession(Context context, String url) {
		setInitSessionUrl(url);
		AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
		String session_request_id = EncrypUtils.getUUId();
		String session_id = PreferenceAppService.getInstance().getItemAndroid(
				SESSION_ID);
		String session_card = PreferenceAppService.getInstance()
				.getItemAndroid(SESSION_CARD).equals("0") ? EncrypUtils
				.getUid(context) : PreferenceAppService.getInstance().getItemAndroid(
				SESSION_CARD);
		String session_key = PreferenceAppService.getInstance()
				.getItemAndroid(SESSION_KEY).equals("0") ? "session_key"
				: PreferenceAppService.getInstance().getItemAndroid(SESSION_KEY);
		String sign_utc_time = EncrypUtils.getUTC();
		String expired_time_offset = "1800";
		String auth_string = EncrypUtils.getAuthString(session_request_id,
				session_id, session_card, sign_utc_time, expired_time_offset);
		String signing_key = EncrypUtils
				.getSigningKey(auth_string, session_key);
		String interference = EncrypUtils.getUUId();
		auth_string = EncrypUtils.getAuthString(auth_string, interference);
		String session_sign = EncrypUtils.getSigningKey(auth_string,
				signing_key);
		auth_string = EncrypUtils.getAuthString(auth_string, session_sign);

		RequestParams requestParams = new RequestParams();
		client.addHeader("Authorization", auth_string);

		// 执行post方法
		client.post(url, requestParams, new AsyncHttpResponseHandler() {

			@Override
			public void onFailure(int statusCode, Header[] headers,
					byte[] responseBody, Throwable error) {
				error.printStackTrace();// 把错误信息打印出轨迹来
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					byte[] responseBody) {
				if (statusCode == 200) {
					String resultString = new String(responseBody);
					Logger.i(TAG, "resultString:" + resultString);
					JSONObject jsonObject_al = null;
					JSONObject jsonObject_data = null;
					SessionData sessionData = null;
					try {
						jsonObject_al = new JSONObject(resultString);
						if (jsonObject_al.getBoolean("state") == true
								&& jsonObject_al.getString("type").equals(
										"update")) {
							jsonObject_data = jsonObject_al
									.getJSONObject("session_data");
							sessionData = new SessionData();
							sessionData.session_id_name = jsonObject_data
									.getString("session_id_name");
							sessionData.session_card_name = jsonObject_data
									.getString("session_card_name");
							sessionData.session_key_name = jsonObject_data
									.getString("session_key_name");
							sessionData.session_sign_name = jsonObject_data
									.getString("session_sign_name");
							sessionData.session_prefix = jsonObject_data
									.getString("session_prefix");
							sessionData.session_id = jsonObject_data
									.getString("session_id");
							sessionData.session_card = jsonObject_data
									.getString("session_card");
							sessionData.session_key = jsonObject_data
									.getString("session_key");
							sessionData.session_sign = jsonObject_data
									.getString("session_sign");
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
					if (sessionData != null) {
						Logger.i(TAG, "session_id_name:"
								+ sessionData.session_id_name);
						Logger.i(TAG, "session_card_name:"
								+ sessionData.session_card_name);
						Logger.i(TAG, "session_key_name:"
								+ sessionData.session_key_name);
						Logger.i(TAG, "session_sign_name:"
								+ sessionData.session_sign_name);
						Logger.i(TAG, "session_prefix:"
								+ sessionData.session_prefix);
						Logger.i(TAG, "session_id:" + sessionData.session_id);
						Logger.i(TAG, "session_card:"
								+ sessionData.session_card);
						Logger.i(TAG, "session_key:" + sessionData.session_key);
						Logger.i(TAG, "session_sign:"
								+ sessionData.session_sign);

						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_ID_NAME, sessionData.session_id_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_CARD_NAME,
								sessionData.session_card_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_KEY_NAME, sessionData.session_key_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_SIGN_NAME,
								sessionData.session_sign_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_HEADERS_PREFIX,
								sessionData.session_prefix);
						PreferenceAppService.getInstance().setItemAndroid(SESSION_ID,
								sessionData.session_id);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_CARD, sessionData.session_card);
						PreferenceAppService.getInstance().setItemAndroid(SESSION_KEY,
								sessionData.session_key);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_SIGN, sessionData.session_sign);
					}
				}
			}
		});
	}

	/**
	 * 初始化网络请求动态路径
	 * 
	 * @param context
	 * @param url
	 */
	public static void initSession(Context context,
			final InitCallback initCallback) {
		AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
		String url = PreferenceAppService.getInstance().getItemAndroid(SESSION_URL)
				.equals("0") ? "" : PreferenceAppService.getInstance().getItemAndroid(
				SESSION_URL);
		String session_request_id = EncrypUtils.getUUId();
		String session_id = PreferenceAppService.getInstance().getItemAndroid(
				SESSION_ID);
		String session_card = PreferenceAppService.getInstance()
				.getItemAndroid(SESSION_CARD).equals("0") ? EncrypUtils
				.getUid(context) : PreferenceAppService.getInstance().getItemAndroid(
				SESSION_CARD);
		String session_key = PreferenceAppService.getInstance()
				.getItemAndroid(SESSION_KEY).equals("0") ? "session_key"
				: PreferenceAppService.getInstance().getItemAndroid(SESSION_KEY);
		String sign_utc_time = EncrypUtils.getUTC();
		String expired_time_offset = "1800";
		String auth_string = EncrypUtils.getAuthString(session_request_id,
				session_id, session_card, sign_utc_time, expired_time_offset);
		String signing_key = EncrypUtils
				.getSigningKey(auth_string, session_key);
		String interference = EncrypUtils.getUUId();
		auth_string = EncrypUtils.getAuthString(auth_string, interference);
		String session_sign = EncrypUtils.getSigningKey(auth_string,
				signing_key);
		auth_string = EncrypUtils.getAuthString(auth_string, session_sign);

		RequestParams requestParams = new RequestParams();
		client.addHeader("Authorization", auth_string);

		// 执行post方法
		client.post(url, requestParams, new AsyncHttpResponseHandler() {

			@Override
			public void onFailure(int statusCode, Header[] headers,
					byte[] responseBody, Throwable error) {
				error.printStackTrace();// 把错误信息打印出轨迹来
				initCallback.onFailed();
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					byte[] responseBody) {
				if (statusCode == 200) {
					String resultString = new String(responseBody);
					Logger.i(TAG, "resultString:" + resultString);
					JSONObject jsonObject_al = null;
					JSONObject jsonObject_data = null;
					SessionData sessionData = null;
					try {
						jsonObject_al = new JSONObject(resultString);
						if (jsonObject_al.getBoolean("state") == true
								&& jsonObject_al.getString("type").equals(
										"update")) {
							jsonObject_data = jsonObject_al
									.getJSONObject("session_data");
							sessionData = new SessionData();
							sessionData.session_id_name = jsonObject_data
									.getString("session_id_name");
							sessionData.session_card_name = jsonObject_data
									.getString("session_card_name");
							sessionData.session_key_name = jsonObject_data
									.getString("session_key_name");
							sessionData.session_sign_name = jsonObject_data
									.getString("session_sign_name");
							sessionData.session_prefix = jsonObject_data
									.getString("session_prefix");
							sessionData.session_id = jsonObject_data
									.getString("session_id");
							sessionData.session_card = jsonObject_data
									.getString("session_card");
							sessionData.session_key = jsonObject_data
									.getString("session_key");
							sessionData.session_sign = jsonObject_data
									.getString("session_sign");
						}
					} catch (JSONException e) {
						initCallback.onFailed();
						e.printStackTrace();
					}
					if (sessionData != null) {
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_ID_NAME, sessionData.session_id_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_CARD_NAME,
								sessionData.session_card_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_KEY_NAME, sessionData.session_key_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_SIGN_NAME,
								sessionData.session_sign_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_HEADERS_PREFIX,
								sessionData.session_prefix);
						PreferenceAppService.getInstance().setItemAndroid(SESSION_ID,
								sessionData.session_id);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_CARD, sessionData.session_card);
						PreferenceAppService.getInstance().setItemAndroid(SESSION_KEY,
								sessionData.session_key);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_SIGN, sessionData.session_sign);
						initCallback.onSuccess();
					}
				}
			}
		});
	}

	/**
	 * 初始化网络请求动态路径
	 * 
	 * @param context
	 * @param url
	 */
	public static void initSession(Context context) {
		AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
		String url = PreferenceAppService.getInstance().getItemAndroid(SESSION_URL)
				.equals("0") ? "" : PreferenceAppService.getInstance().getItemAndroid(
				SESSION_URL);
		String session_request_id = EncrypUtils.getUUId();
		String session_id = PreferenceAppService.getInstance().getItemAndroid(
				SESSION_ID);
		String session_card = PreferenceAppService.getInstance()
				.getItemAndroid(SESSION_CARD).equals("0") ? EncrypUtils
				.getUid(context) : PreferenceAppService.getInstance().getItemAndroid(
				SESSION_CARD);
		String session_key = PreferenceAppService.getInstance()
				.getItemAndroid(SESSION_KEY).equals("0") ? "session_key"
				: PreferenceAppService.getInstance().getItemAndroid(SESSION_KEY);
		String sign_utc_time = EncrypUtils.getUTC();
		String expired_time_offset = "1800";
		String auth_string = EncrypUtils.getAuthString(session_request_id,
				session_id, session_card, sign_utc_time, expired_time_offset);
		String signing_key = EncrypUtils
				.getSigningKey(auth_string, session_key);
		String interference = EncrypUtils.getUUId();
		auth_string = EncrypUtils.getAuthString(auth_string, interference);
		String session_sign = EncrypUtils.getSigningKey(auth_string,
				signing_key);
		auth_string = EncrypUtils.getAuthString(auth_string, session_sign);

		RequestParams requestParams = new RequestParams();
		client.addHeader("Authorization", auth_string);

		// 执行post方法
		client.post(url, requestParams, new AsyncHttpResponseHandler() {

			@Override
			public void onFailure(int statusCode, Header[] headers,
					byte[] responseBody, Throwable error) {
				error.printStackTrace();// 把错误信息打印出轨迹来
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					byte[] responseBody) {
				if (statusCode == 200) {
					String resultString = new String(responseBody);
					Logger.i(TAG, "resultString:" + resultString);
					JSONObject jsonObject_al = null;
					JSONObject jsonObject_data = null;
					SessionData sessionData = null;
					try {
						jsonObject_al = new JSONObject(resultString);
						if (jsonObject_al.getBoolean("state") == true
								&& jsonObject_al.getString("type").equals(
										"update")) {
							jsonObject_data = jsonObject_al
									.getJSONObject("session_data");
							sessionData = new SessionData();
							sessionData.session_id_name = jsonObject_data
									.getString("session_id_name");
							sessionData.session_card_name = jsonObject_data
									.getString("session_card_name");
							sessionData.session_key_name = jsonObject_data
									.getString("session_key_name");
							sessionData.session_sign_name = jsonObject_data
									.getString("session_sign_name");
							sessionData.session_prefix = jsonObject_data
									.getString("session_prefix");
							sessionData.session_id = jsonObject_data
									.getString("session_id");
							sessionData.session_card = jsonObject_data
									.getString("session_card");
							sessionData.session_key = jsonObject_data
									.getString("session_key");
							sessionData.session_sign = jsonObject_data
									.getString("session_sign");
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
					if (sessionData != null) {
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_ID_NAME, sessionData.session_id_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_CARD_NAME,
								sessionData.session_card_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_KEY_NAME, sessionData.session_key_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_SIGN_NAME,
								sessionData.session_sign_name);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_HEADERS_PREFIX,
								sessionData.session_prefix);
						PreferenceAppService.getInstance().setItemAndroid(SESSION_ID,
								sessionData.session_id);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_CARD, sessionData.session_card);
						PreferenceAppService.getInstance().setItemAndroid(SESSION_KEY,
								sessionData.session_key);
						PreferenceAppService.getInstance().setItemAndroid(
								SESSION_SIGN, sessionData.session_sign);
					}
				}
			}
		});
	}

	public interface InitCallback {
		void onSuccess();

		void onFailed();
	}
}
